const express = require('express')
const router = express.Router();
const mySql = require('../../config/db')
// 处理时间搓的
// const moment = require('./node_modules/moment')
// 创建个人求职意向
router.post('/', (req, res) => {
  const reRes = {
    status: 200,
    message: "",
    data: null
  }
  const { citys, industry, position, salary, scale, ensure} = req.body;
  const uId = req.headers.authorization.replace(/[^0-9]/ig, "")
  // 判断该用户名是否已经创建
  const sqlSelect = "select * from job_intention where u_id = ?";
  mySql.query(sqlSelect, [uId], (error, data) => {
    if (error) {
      reRes.message = '新增失败';
      reRes.data = null;
      res.json(reRes)
    } else {
      if (data.length === 0) {
        // 没有创建
        let time = Math.round((new Date().getTime()) / 1000);
        const sqlInsert = 'insert into job_intention(u_id,industry,position,salary,citys,ensure,scale,time) values(?,?,?,?,?,?,?,?)';
        mySql.query(sqlInsert, [uId, industry, position, salary, citys, ensure, scale, time], (err, data) => {
          if (err) {
            reRes.message = '新增失败1';
            reRes.data = null;
            res.json(reRes)
          } else {
            // 判断是否执行成功
            if (data.affectedRows === 1) {
              reRes.message = '新增成功';
              reRes.data = data;
              res.json(reRes)
            } else {
              reRes.message = '新增失败2';
              reRes.data = null;
              res.json(reRes)
            }
          }
        })
      } else {
        reRes.message = '已经创建，不能在创建';
        reRes.data = null;
        res.json(reRes)
        // 已经创建过
      }
    }
  })
})
// 编辑个人信息
router.put('/', (req, res) => {
  const { id, citys, industry, position, salary, scale, ensure } = req.body;
  const reRes = {
    status: 200,
    message: "",
    data: null
  }
  const uId = req.headers.authorization.replace(/[^0-9]/ig, "")
  const sqlSelect = "select u_id from job_intention where u_id = ?";
  mySql.query(sqlSelect, [uId], (err, data) => {
    if (err) {
      reRes.message = "编辑失败1"
      reRes.data = null;
      res.json(reRes);
    } else {
      if (data.length === 0 || parseInt(uId) !== data[0].u_id) {
        reRes.message = "编辑失败2"
        reRes.data = null;
        res.json(reRes);
      } else {
        let time = Math.round((new Date().getTime()) / 1000);
        sqlUpdate = `update job_intention set 
        industry = '${industry}', position = '${position}',salary = '${salary}',citys = '${citys}',
        ensure = '${ensure}',scale = '${scale}',time= ${time} where id = ${id}`;
        mySql.query(sqlUpdate, (err, data) => {
          if (err) {
            // 修改失败
            reRes.message = '修改失败';
            reRes.data = null;
            res.json(reRes)
          } else {
            // 判断是否执行成功
            if (data.affectedRows === 1) {
              reRes.message = '修改成功';
              reRes.data = data;
              res.json(reRes)
            }
          }
        })
      }
    }
  })
})
// 获取个人基础信息列表
router.get('/', (req, res) => {
  const reRes = {
    status: 200,
    message: "",
    data: null
  }
  const sqlList = 'select * from job_intention order by id desc';
  mySql.query(sqlList, (err, data) => {
    if (err) {
      reRes = {
        status: 200,
        message: "获取失败",
        data: null
      }
      res.json(reRes)
    } else {
      if (data.length > 0) {
        reRes.message = '获取成功';
        reRes.data = data;
        res.json(reRes);
      } else {
        reRes.message = '获取成功';
        reRes.data = [];
        res.json(reRes);
      }
    }
  })

})
// 删除管理员列表
router.delete('/', (req, res) => {
  const reRes = {
    status: 200,
    message: "",
    data: null
  }
  const id = req.query.id;
  const uId = req.headers.authorization.replace(/[^0-9]/ig, "")
  const sqlSelect = "select * from job_intention where u_id = ?";
  mySql.query(sqlSelect, [uId], (err, data) => {
    if (err) {
      reRes.message = "删除失败"
      reRes.data = null;
      res.json(reRes);
    } else {
      if (data.length === 0) {
        reRes.message = "文件不存在"
        reRes.data = null;
        res.json(reRes);
      } else {
        const sqlDelete = `delete from job_intention where id = ${id}`;
        mySql.query(sqlDelete, (err, data) => {
          if (err) {
            reRes.message = "删除失败"
            res.json(reRes);
          } else {
            // 判断是否执行成功 
            if (data.affectedRows == 1) {
              reRes.message = "删除成功"
              reRes.data = data;
              res.json(reRes);
            } else {
              reRes.message = "删除失败"
              res.json(reRes);
            }
          }
        })
      }
    }
  })
})
module.exports = router;